<template>
  <div class="wrapper">
    <div class="header">
      <div class="title">
        <span>One Api</span>
      </div>
      <div v-if="!nickName" class="operation" @click="dialogVisible = true">
        <span>登录</span>
      </div>
      <div v-else class="user">
        <el-dropdown @command="handleLogout">
          <span style="font-size: 16px;">欢迎你,{{ nickName }}</span>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item command="logout">退出登录</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
    </div>
    <!-- 登录框 -->
    <el-dialog :visible.sync="dialogVisible" width="30%" class="login">
      <div slot="title">
        <h2>One Api 登录</h2>
      </div>
      <el-form :model="form" :rules="rules" ref="loginForm">
        <el-form-item prop="username">
          <el-input v-model="form.username" autocomplete="off" prefix-icon="el-icon-user-solid" clearable></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="form.password" type="password" auto-complete="off" prefix-icon="el-icon-lock"
            show-password />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="width:100%;" @click="submitForm" v-loading="loading">
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { userLogin, userLogout } from '@/api/UserRequest.js'
import { mapState } from 'vuex'
export default {
  data() {
    return {
      loading: false,
      dialogVisible: false,
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 4, message: '长度不能少于4位', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 8, message: '长度不能少于8位', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    handleClick() {
      this.$router.push('/')
    },
    submitForm() {
      this.loading = true
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          userLogin(this.form).then(resp => {
            this.loading = false
            this.$store.commit('setNickName', resp.data.nickName)
            this.$store.commit('setUserRole', resp.data.userRole)
            this.$message.success('登录成功')
            this.dialogVisible = false
          }).catch(err => {
            this.loading = false
            this.$message.error(err)
          })
        } else {
          this.loading = false
          return false
        }
      })
    },
    handleLogout() {
      userLogout().then(resp => {
        this.$message.success('退出登录')
        this.$store.commit('removeNickName')
        this.$store.commit('removeUserRole')
        this.$router.push('/home')
      })
    }
  },
  computed: {
    ...mapState(['nickName', 'userRole'])
  }
}
</script>

<style lang="scss" scoped>
.wrapper {
  height: 100%;
  background-color: #000;
  font-size: 20px;
  line-height: 60px;

  .header {
    height: 100%;
    width: 80%;
    margin: 0 auto;
    position: relative;
  }

  .title {
    position: absolute;
    left: 0;
    padding: 0 5px;
    cursor: pointer;

    &:hover>span {
      color: red;
    }

    span {
      color: #fff;
      font-weight: bold;
    }
  }

  .operation {
    position: absolute;
    width: 100px;
    right: 0;
    color: #fff;
    padding-right: 5px;
    cursor: pointer;
    text-align: right;

    &:hover {
      color: red;
    }
  }

  .user {
    position: absolute;
    width: 150px;
    right: 0;
    padding-right: 5px;
    cursor: pointer;
    text-align: right;

    span {
      color: #e01111;
    }
  }

  .login {
    h2 {
      margin: 0;
    }
  }
}

.el-dropdown-menu__item {
  background-color: #000;
  color: white;

  &:hover {
    background-color: #000;
    color: red;
  }
}

.popper_arrow {
  background-color: #000;
}
</style>
